﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.LoginConfig;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace EkStudio.com.ui.login
{
    /// <summary>
    /// 页面登录
    /// </summary>
    public partial class DefaultLoginForm : Form
    {
        private WebLoginCfg config = null;
        public DefaultLoginForm()
        {
            InitializeComponent();

            this.InitLogin();  
        }

        private void InitLogin()
        {
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "UserConfig.xml";
                if (File.Exists(path))
                {
                    xmldoc.Load(path);
                    string xml = xmldoc.OuterXml;
                    if (xml != "")
                    {
                        StringReader XmlReader = new StringReader(xml);
                        XmlSerializer xs = new XmlSerializer(typeof(WebLoginCfg));
                        config = (WebLoginCfg)xs.Deserialize(XmlReader);
                        XmlReader.Dispose();
                    }
                }

            }
            catch
            {
                MessageBox.Show("加载文件异常");
            }
            
            if (config != null && config.Item.Count > 0)
            {
                int index = 0;
                foreach(WebLoginItem it in config.Item)
                {
                    this.InputUrl.Properties.Items.Add(it.LoginUrl);
                    if (it.LoginDefault)
                    {
                        this.InputUrl.SelectedIndex = index;
                        this.InputLinkType.SelectedIndex = it.LoginType;
                        this.InputUser.Text = it.LoginUser;
                        this.InputPwd.Text = it.LoginPwd;
                    }
                    index++;
                }
            }
        }

        public String LoginType
        {
            get { return this.InputLinkType.Text.Trim(); }
        }
        public String LoginURL
        {
            get { return this.InputUrl.Text.Trim(); }
        }

        public String LoginUser
        {
            get { return this.InputUser.Text.Trim(); }
        }

        public String LoginPwd
        {
            get { return this.InputPwd.Text.Trim(); }
        }

        public bool LoginRemind
        {
            get { return this.InputRemind.Checked; }
        }

        private void InputUrl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (config != null && config.Item.Count > 0)
            {
                foreach (WebLoginItem it in config.Item)
                {
                    string text = this.InputUrl.SelectedItem.ToString();
                    if(it.LoginUrl == text)
                    {
                        this.InputLinkType.SelectedIndex = it.LoginType;
                        this.InputUser.Text = it.LoginUser;
                        this.InputPwd.Text = it.LoginPwd;
                    }
                }
            }
        }

        private void SaveConfig()
        {
            
           
            XmlDocument xmldoc = new XmlDocument();
            XmlSerializer xs = new XmlSerializer(typeof(WebLoginCfg));
            try
            {
                MemoryStream stream = new MemoryStream();
                XmlWriterSettings setting = new XmlWriterSettings();
                setting.Encoding = new UTF8Encoding(false);
                setting.Indent = true;
                XmlWriter writer = XmlWriter.Create(stream, setting);
                xs.Serialize(writer, config);
                String xml = Encoding.UTF8.GetString(stream.ToArray());
                xmldoc.LoadXml(xml);
                xmldoc.Save(AppDomain.CurrentDomain.BaseDirectory + "UserConfig.xml");
                stream.Dispose();
            }
            finally
            {
            }
        }

        private void BtLogin_Click(object sender, EventArgs e)
        {
            if (this.InputRemind.Checked&&this.InputUrl.Text.Trim()!="")
            {
                if (config == null) config = new WebLoginCfg();
                config.AddItem(this.InputLinkType.SelectedIndex, this.InputUrl.Text.Trim(), this.InputUser.Text.Trim(), this.InputPwd.Text.Trim(), true);
                SaveConfig();
            }
            
        }
        

        private void InputUrl_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if(e.Button.Tag.ToString() == "Clear")
            {
                DialogResult dr = XtraMessageBox.Show("您确认删除当前链接?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    String url = this.InputUrl.Text.Trim();
                    if (config != null && config.Item.Count > 0)
                    {
                        config.RemoveItem(url);
                        this.InputUrl.Properties.Items.Remove(url);
                        this.InputUrl.Text = "";
                        this.InputLinkType.SelectedIndex = 0;
                        this.InputUser.Text = "";
                        this.InputPwd.Text = "";
                        SaveConfig();
                    }
                }
            }
            
        }
    }
}
